package org.dfl.plugin.editor.markers;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;

/**
 * Container for markers assigned with specific resource and owner object 	 
 * @author Piotr Wlodarczyk
 */
public class ResourceMarkersContainer {
	private IResource resource;
	private Object owner;
	private Map<Long,IMarker> markers = new HashMap<Long,IMarker>();

	public ResourceMarkersContainer(Object owner, IResource resource) {
		super();
		this.resource = resource;
		this.owner = owner;
		GlobalMarkersManager.registerMarkersContainer(this);
	}
	
	public void unregister(){
		GlobalMarkersManager.unregisterMarkersContainer(this);
	}
	/***************************************************  markers ********************************************/
	protected void addMarker(IMarker marker) {
		this.markers.put(marker.getId(),marker);
	}
	
	protected void removeMarker(IMarker marker) {
		this.markers.remove(marker);
	}
	
	public void clearMarkers() {
		Collection<IMarker> mm = markers.values();
		for(IMarker m : mm){
			try {m.delete();} catch (CoreException e) {}
		}
		this.markers.clear();
	}
	
	/**
	 * adds new problem marker
	 * @param msg			- message
	 * @param location		- marker location
	 * @param priority		- priority
	 * @param severity		- severity
	 */
	protected void addProblemMarker(String msg, String location, int priority, int severity ){
		try {
			IMarker m = resource.createMarker(IMarker.PROBLEM);
			m.setAttribute(IMarker.MESSAGE, msg);
			m.setAttribute(IMarker.PRIORITY, priority);
			m.setAttribute(IMarker.SEVERITY, severity);
			m.setAttribute(IMarker.LOCATION, location);
			addMarker(m);
		} catch (CoreException e) {
		}	
	}
	
	// --------------- getters ---------------------------//
	protected Object getOwner() {
		return owner;
	}

	protected IResource getResource() {
		return resource;
	}

	public Map<Long, IMarker> getMarkers() {
		return markers;
	}
}
